package com.garlic.face.jlf.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.garlic.face.jlf.R;


/**
 * Created by lyq on 2017/11/30.
 * 播放声音的工具类
 */

public class SoundPlayUtil {

    public static final int VOICE_SELETE_PAY = 1;
    public static final int VOICE_GOOD_NOT_FOUND = 2;
    public static final int VOICE_VIP_NUMBER_IN = 3;
    public static final int VOICE_PAY_FAULT = 4;
    public static final int VOICE_PAY_SUCCESS = 5;
    public static final int VOICE_SCAN_PAYCODE = 6;
    public static final int VOICE_SCAN_BARCODE = 7;

    private static SoundPlayUtil mSoundPlayUtil;
    private SoundPool mSoundPlayer;
    private boolean isOpen = true; // 是否播放

    /**
     * 单例获取各种信息
     * @return
     */
    public static SoundPlayUtil getInstance(){
        if (null == mSoundPlayUtil) {
            synchronized (SoundPlayUtil.class) {
                if (null == mSoundPlayUtil) {
                    mSoundPlayUtil = new SoundPlayUtil();
                }
            }
        }
        return mSoundPlayUtil;
    }

    /**
     * 初始化
     * @param context
     * @return
     */
    public SoundPlayUtil init(Context context){
        if (null == mSoundPlayer) {
            mSoundPlayer = new SoundPool(1,
                    AudioManager.STREAM_SYSTEM, 0);
        }
        mSoundPlayer.load(context, R.raw.voice_select_pay, VOICE_SELETE_PAY); // "请选择付款方式"
        mSoundPlayer.load(context, R.raw.voice_good_notfound, VOICE_GOOD_NOT_FOUND); // "找不到扫描的商品"
        mSoundPlayer.load(context, R.raw.voice_vip_number_in, VOICE_VIP_NUMBER_IN); // "请输入或扫描您的会员卡号"
        mSoundPlayer.load(context, R.raw.voice_pay_fault, VOICE_PAY_FAULT); // "支付失败请重新支付"
        mSoundPlayer.load(context, R.raw.voice_pay_success, VOICE_PAY_SUCCESS); // "请拿好您的小票，欢迎再次光临"
        mSoundPlayer.load(context, R.raw.voice_please_scan_paycode, VOICE_SCAN_PAYCODE); // "请扫描付款码"
        mSoundPlayer.load(context, R.raw.voice_please_scan_barcode, VOICE_SCAN_BARCODE); // "请扫描商品条码"

        return mSoundPlayUtil;
    }

    /**
     * 打开语音
     */
    public void openSound(){
        isOpen = true;
    }

    /**
     * 关闭语音
     */
    public void closeSound(){
        isOpen = false;
    }

    /**
     * 释放资源
     */
    public void relase(){
        if (null != mSoundPlayer) {
            mSoundPlayer.release();
            mSoundPlayer = null;
        }
    }

    /**
     * 播放音频
     */
    public void play(int n){
        if (null != mSoundPlayer && isOpen) {
            mSoundPlayer.play(n, 1, 1, 0, 0, 1);
        }
    }
}
